<template>
  <div class="goods-image">
    <!-- 放大图 -->
    <div
      class="large"
       v-show="!isOutside"
      :style="[{
        backgroundImage: `url(${imageList[currIndex]})`,
        backgroundPositionX: `${x}px`,
        backgroundPositionY: `${y}px`,
      }]"
    ></div>

    <!-- 大图 -->
    <div class="middle">
      <img :src="imageList[currIndex]" alt="" ref="target" />
      <!-- 蒙层滑块 -->
      <!-- 用v-show性能更好，因为实时高速渲染 -->
      <div
        class="layer"
        v-show="!isOutside"
        :style="{ top: top + 'px', left: left + 'px' }"
      ></div>
    </div>

    <!-- 小图列表 -->
    <ul class="small">
      <li v-for="(img, i) in imageList" :key="i" @mouseenter="currIndex = i">
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch, toRefs } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',

  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },

  setup () {
    // 鼠标经过事件
    const currIndex = ref(0)

    // vueuse获取鼠标相对某元素的坐标
    // elementX, elementY：相对目标元素的坐标；x,y:相对浏览器的坐标,
    // elementX, elementY是响应式数据
    // isOutside：boolean，是否在盒子外
    const target = ref(null)
    const { elementX, elementY, isOutside } = useMouseInElement(target)

    /**
     * 图片放大效果
     * 根据鼠标位置确定蒙层位置,鼠标在蒙层中心
     * 1.蒙层是绝对定位，top=elementY-1/2*蒙层高度,left=elementX-1/2*蒙层宽度;
     * 2.鼠标移动位置超出蒙层在边界时中心点组成的正方形，固定蒙层的top,left为零
     * 蒙层所在位置图片放大两倍
     * background-position为负值，大小为top,left的2倍
     */
    const left = ref(0)
    const top = ref(0)
    const bg = ref({ x: 0, y: 0 })
    // 监听数据变化，监听两个值，写成数组
    watch([elementX, elementY], () => {
      if (elementX.value > 100 && elementX.value < 300) {
        left.value = elementX.value - 100
      } else if (elementX.value < 100) {
        left.value = 0
      } else {
        left.value = 200
      }

      if (elementY.value > 100 && elementY.value < 300) {
        top.value = elementY.value - 100
      } else if (elementY.value < 100) {
        top.value = 0
      } else {
        top.value = 200
      }

      bg.value.x = -left.value * 2// 先确定背景图位置，再放大背景图
      bg.value.y = -top.value * 2
    })

    return {
      currIndex,
      target,
      elementX,
      elementY,
      isOutside,
      top,
      left,
      ...toRefs(bg.value)
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
